﻿using System;
using System.Collections.Generic;
using System.Text;
using ZLDC.DAL.Estate;
using ZLDC.Common.Estate;
using ZLDC.Topo.Model.Estate;

namespace ZLDC.Topo.BLL.Estate
{
                public class quantificationBLL
    {
                                                                                public static void UpdateHousingQuantification(int userId,int deptId ,string deptName,string userName, int isAdd, int isHasKey, int isOnly, int isCtyCont, int isHasPhoto, int isAdd1)
        {
                        int qId = GetTodayQuantification(userId);

            if (qId == 0)
            {
                                                                string outmsg = string.Empty;
                int newid = 0;
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.p_quantification_insert(deptId, deptName, userId, userName, 0, 0, 0, 0, isAdd, 0, isOnly, isCtyCont,
                        isHasKey, isHasPhoto, 0, 0, 0, isAdd1, 0, 0, 0, 0, out outmsg, out newid, db);
                }
            }
            else
            {
                List<string> fieldnames = new List<string>();
                if (isAdd == 1)
                {
                    fieldnames.Add("newhousing");
                }
                if (isAdd1 == 1)
                {
                    fieldnames.Add("mark1");
                }
                if (isHasKey == 1)
                {
                    fieldnames.Add("[key]");
                }
                if (isOnly == 1)
                {
                    fieldnames.Add("only");
                }
                if (isCtyCont == 1)
                {
                    fieldnames.Add("versa");
                }
                if (isHasPhoto == 1)
                {
                    fieldnames.Add("hasphoto");
                }
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.UpdateQuantification(qId, fieldnames.ToArray(), db);
                }
            }
        }

                                                                                                                        public static void UpdateHousingQuantification(int tableId, int userId, int deptId, string deptName, string userName, int isAdd, int isHasKey, int isOnly, int isCtyCont, int isHasPhoto, int isAdd1)
        {
            int typeid = 0;             List<string> fieldnames = new List<string>();          

            if (isAdd == 1)
            {
                fieldnames.Add("newhousing");
                typeid = 5;
            }
            if (isAdd1 == 1)
            {
                fieldnames.Add("mark1");
                typeid = 14;
            }
            if (isHasKey == 1)
            {
                fieldnames.Add("[key]");
                typeid = 9;
            }
            if (isOnly == 1)
            {
                fieldnames.Add("only");
                typeid =7;
            }
            else if (isCtyCont == 1)
            {
                fieldnames.Add("versa");
                typeid = 8;
            }
            else if (isHasPhoto == 1)
            {
                fieldnames.Add("hasphoto");
                typeid = 11;
            }

            int qId = GetTodayQuantification(userId);

            if (qId == 0)
            {
                string outmsg = string.Empty;
                int newid = 0;
                using (_Database db = new _Database("estate"))
                {                    
                    QuantificationDAL.p_quantification_insert(typeid, tableId, deptId, deptName, userId, userName, 0, 0, 0, 0, isAdd, 0, isOnly, isCtyCont, isHasKey, isHasPhoto, 0, 0, 0, isAdd1, 0, 0, 0, 0, out outmsg, out newid, db);
                }
            }
            else
            {                
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.UpdateQuantification(typeid, tableId, userId, deptId, deptName, userName, qId, fieldnames.ToArray(), db);
                }
            }
        }

                                                                                public static void UpdateHousingQuantificationByUserName(int hasKey, int isOnly, int ctyCont, string keepKey, string onlyUser, string ctyContUser)
        {
                        if (hasKey == 1)
            {
                UserInfo user = UserBLL.GetUserInfoBysUserName(keepKey);
                if (user.Id > 0)
                {
                    quantificationBLL.UpdateHousingQuantification(user.Id, user.Dept_id, user.DeptName, user.Name, 0, 1, 0, 0, 0, 0);
                }
            }
                        if (isOnly == 1)
            {
                UserInfo user = UserBLL.GetUserInfoBysUserName(onlyUser);
                if (user.Id > 0)
                {
                    quantificationBLL.UpdateHousingQuantification(user.Id, user.Dept_id, user.DeptName, user.Name, 0, 0, 1, 0, 0, 0);
                }
            }
                        if (ctyCont == 1)
            {
                UserInfo user = UserBLL.GetUserInfoBysUserName(ctyContUser);
                if (user.Id > 0)
                {
                    quantificationBLL.UpdateHousingQuantification(user.Id, user.Dept_id, user.DeptName, user.Name, 0, 0, 0, 1, 0, 0);
                }
            }


        }

                                                                                        public static void UpdateHousingQuantificationByUserName(int tableId, int hasKey, int isOnly, int ctyCont, string keepKey, string onlyUser, string ctyContUser)
        {

                        if (hasKey == 1)
            {
                UserInfo user = UserBLL.GetUserInfoBysUserName(keepKey);
                if (user.Id > 0)
                {
                    quantificationBLL.UpdateHousingQuantification(tableId, user.Id, user.Dept_id, user.DeptName, user.Name, 0, 1, 0, 0, 0, 0);
                }
            }
                        if (isOnly == 1)
            {
                UserInfo user = UserBLL.GetUserInfoBysUserName(onlyUser);
                if (user.Id > 0)
                {
                    quantificationBLL.UpdateHousingQuantification(tableId, user.Id, user.Dept_id, user.DeptName, user.Name, 0, 0, 1, 0, 0, 0);
                }
            }
                        if (ctyCont == 1)
            {
                UserInfo user = UserBLL.GetUserInfoBysUserName(ctyContUser);
                if (user.Id > 0)
                {
                    quantificationBLL.UpdateHousingQuantification(tableId, user.Id, user.Dept_id, user.DeptName, user.Name, 0, 0, 0, 1, 0, 0);
                }
            }
        }
        
                                                private static int GetTodayQuantification(int userId)
        {
            int qId = 0;
            string today = DateTime.Today.ToString("yyyy-MM-dd");
            using (_Database db = new _Database("estate"))
            {
                qId = QuantificationDAL.isExit(today, userId, db);
            }
            return qId;
        }

                                public static void UpdateHousingFollowQuantification(string followType, int userId, int deptId, string userName, string deptName)
        {
                        int qId = GetTodayQuantification(userId);

            if (qId == 0)
            {
                                                                string outmsg = string.Empty;
                int newid = 0;
                using (_Database db = new _Database("estate"))
                {
                   
                    int self = 0;
                    int looked = 0;
                    if (followType.Equals("自看房"))
                    {
                        self = 1;
                    }
                   
                    
                                                                                                    QuantificationDAL.p_quantification_insert(deptId, deptName, userId, userName, 1, 0, looked, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, self, 0, 0, 0, 0, 0, out outmsg, out newid, db);
                    
                    
                }
            }
            else
            {
                List<string> fieldnames = new List<string>();
                if (followType.Equals("自看房"))
                {
                    fieldnames.Add("[self]");
                }
                
                fieldnames.Add("hfollow");
                
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.UpdateQuantification(qId, fieldnames.ToArray(), db);
                }
            }
        }

                                public static void UpdateHousingAlertQuantification(string AlertType)
        {
            int userId = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            int qId = GetTodayQuantification(userId);

            if (qId == 0)
            {
                int deptId = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);
                string deptName = _Utility.GetInfoBySession().m_sDeptName;
                string userName = _Utility.GetInfoBySession().m_sUserName;
                string outmsg = string.Empty;
                int newid = 0;
                using (_Database db = new _Database("estate"))
                {
                    if (AlertType.Equals("约看"))
                    {
                        QuantificationDAL.p_quantification_insert(deptId, deptName, userId, userName, 0, 0, 1, 0, 0, 0, 0, 0,
                            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, out outmsg, out newid, db);
                    }
                }
            }
            else
            {
                List<string> fieldnames = new List<string>();
                if (AlertType.Equals("约看"))
                {
                    fieldnames.Add("[look]");
                }
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.UpdateQuantification(qId, fieldnames.ToArray(), db);
                }
            }
        }

                                public static void UpdateTelQuantification()
        {
            int userId = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            int qId = GetTodayQuantification(userId);

            if (qId == 0)
            {
                int deptId = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);
                string deptName = _Utility.GetInfoBySession().m_sDeptName;
                string userName = _Utility.GetInfoBySession().m_sUserName;
                string outmsg = string.Empty;
                int newid = 0;
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.p_quantification_insert(deptId, deptName, userId, userName, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 1, 0, 0, 0, 0, 0, 0, 0, 0, out outmsg, out newid, db);
                }
            }
            else
            {
                List<string> fieldnames = new List<string>();
                fieldnames.Add("[telphone]");
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.UpdateQuantification(qId, fieldnames.ToArray(), db);
                }
            }
        }

                                public static void UpdateHasPhotoQuantification()
        {
            int userId = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            int qId = GetTodayQuantification(userId);

            if (qId == 0)
            {
                int deptId = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);
                string deptName = _Utility.GetInfoBySession().m_sDeptName;
                string userName = _Utility.GetInfoBySession().m_sUserName;
                string outmsg = string.Empty;
                int newid = 0;
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.p_quantification_insert(deptId, deptName, userId, userName, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 1, 0, 0, 0, 0, 0, 0, 0, out outmsg, out newid, db);
                }
            }
            else
            {
                List<string> fieldnames = new List<string>();
                fieldnames.Add("[hasphoto]");
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.UpdateQuantification(qId, fieldnames.ToArray(), db);
                }
            }
        }

                                public static void UpdatePhotoesQuantification()
        {
            int userId = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            int qId = GetTodayQuantification(userId);

            if (qId == 0)
            {
                int deptId = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);
                string deptName = _Utility.GetInfoBySession().m_sDeptName;
                string userName = _Utility.GetInfoBySession().m_sUserName;
                string outmsg = string.Empty;
                int newid = 0;
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.p_quantification_insert(deptId, deptName, userId, userName, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 1, 0, 0, 0, 0, 0, 0, out outmsg, out newid, db);
                }
            }
            else
            {
                List<string> fieldnames = new List<string>();
                fieldnames.Add("[photo]");
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.UpdateQuantification(qId, fieldnames.ToArray(), db);
                }
            }
        }

                                public static void UpdateCustomerQuantification(string tradetype)
        {
            int userId = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            int qId = GetTodayQuantification(userId);

            if (qId == 0)
            {
                int deptId = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);
                string deptName = _Utility.GetInfoBySession().m_sDeptName;
                string userName = _Utility.GetInfoBySession().m_sUserName;
                string outmsg = string.Empty;
                int newid = 0;
                using (_Database db = new _Database("estate"))
                {
                    if (tradetype.Equals("求购"))
                        QuantificationDAL.p_quantification_insert(deptId, deptName, userId, userName, 0, 0, 0, 0, 0, 1, 0, 0,
                            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, out outmsg, out newid, db);
                    else
                        QuantificationDAL.p_quantification_insert(deptId, deptName, userId, userName, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 1, 0, 0, 0, out outmsg, out newid, db);
                }
            }
            else
            {
                List<string> fieldnames = new List<string>();
                if (tradetype.Equals("求购"))
                    fieldnames.Add("[newcustomer]");
                else
                    fieldnames.Add("[mark2]");
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.UpdateQuantification(qId, fieldnames.ToArray(), db);
                }
            }
        }

                                                                                public static void UpdateCustomerQuantification(int tableId, string tradetype)
        {
            int typeid = 0;
            if (tradetype.Equals("求购"))
                typeid = 6;
            else
                typeid = 15;

            int userId = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            int deptId = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);
            string deptName = _Utility.GetInfoBySession().m_sDeptName;
            string userName = _Utility.GetInfoBySession().m_sUserName;

            int qId = GetTodayQuantification(userId);

            if (qId == 0)
            {                
                string outmsg = string.Empty;
                int newid = 0;
                using (_Database db = new _Database("estate"))
                {
                    if (tradetype.Equals("求购"))
                        QuantificationDAL.p_quantification_insert(typeid, tableId, deptId, deptName, userId, userName, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, out outmsg, out newid, db);
                    else
                        QuantificationDAL.p_quantification_insert(typeid, tableId, deptId, deptName, userId, userName, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, out outmsg, out newid, db);
                }
            }
            else
            {
                List<string> fieldnames = new List<string>();
                if (tradetype.Equals("求购"))
                    fieldnames.Add("[newcustomer]");
                else
                    fieldnames.Add("[mark2]");
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.UpdateQuantification(typeid, tableId, userId, deptId, deptName, userName, qId, fieldnames.ToArray(), db);
                }
            }
        }

                                                                                public static void UpdateCustomerQuantification(int tableId, string tradetype, int deptId, string deptName, int userId, string userName)
        {
            int typeid = 0;
            if (tradetype.Equals("求购"))
                typeid = 6;
            else
                typeid = 15;

            int qId = GetTodayQuantification(userId);

            if (qId == 0)
            {
                string outmsg = string.Empty;
                int newid = 0;
                using (_Database db = new _Database("estate"))
                {
                    if (tradetype.Equals("求购"))
                        QuantificationDAL.p_quantification_insert(typeid, tableId, deptId, deptName, userId, userName, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, out outmsg, out newid, db);
                    else
                        QuantificationDAL.p_quantification_insert(typeid, tableId, deptId, deptName, userId, userName, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, out outmsg, out newid, db);
                }
            }
            else
            {
                List<string> fieldnames = new List<string>();
                if (tradetype.Equals("求购"))
                    fieldnames.Add("[newcustomer]");
                else
                    fieldnames.Add("[mark2]");
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.UpdateQuantification(typeid, tableId, userId, deptId, deptName, userName, qId, fieldnames.ToArray(), db);
                }
            }
        }

                                                                                public static void UpdateCustomerChangeQuantification(int tableId, string tradetype, int deptId, string deptName, int userId, string userName)
        {
            int typeid = 0;
            if (tradetype.Equals("求购"))
                typeid = 17;
            else
                typeid = 18;

            int qId = GetTodayQuantification(userId);

            if (qId == 0)
            {
                string outmsg = string.Empty;
                int newid = 0;
                using (_Database db = new _Database("estate"))
                {
                    if (tradetype.Equals("求购"))
                        QuantificationDAL.p_quantification_insert(typeid, tableId, deptId, deptName, userId, userName, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, out outmsg, out newid, db);
                    else
                        QuantificationDAL.p_quantification_insert(typeid, tableId, deptId, deptName, userId, userName, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, out outmsg, out newid, db);
                }
            }
            else
            {
                List<string> fieldnames = new List<string>();
                if (tradetype.Equals("求购"))
                    fieldnames.Add("[mark4]");
                else
                    fieldnames.Add("[mark5]");
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.UpdateQuantification(typeid, tableId, userId, deptId, deptName, userName, qId, fieldnames.ToArray(), db);
                }
            }
        }

                                public static void UpdateCustomerFollowQuantification(string followType)
        {
            int userId = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            int qId = GetTodayQuantification(userId);

            if (qId == 0)
            {
                int deptId = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);
                string deptName = _Utility.GetInfoBySession().m_sDeptName;
                string userName = _Utility.GetInfoBySession().m_sUserName;
                string outmsg = string.Empty;
                int newid = 0;
                using (_Database db = new _Database("estate"))
                {
                    int looked = 0;                         int mark3 = 0;                          if (followType.Equals("带看售房"))
                    {
                        looked = 1;
                    }
                    else if (followType.Equals("带看租房"))
                    {
                        mark3 = 1;
                    }

                    QuantificationDAL.p_quantification_insert(deptId, deptName, userId, userName, 0, 1, 0, looked, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, mark3, 0, 0, out outmsg, out newid, db);
                }
            }
            else
            {
                List<string> fieldnames = new List<string>();
                if (followType.Equals("带看售房"))
                {
                    fieldnames.Add("[looked]");                    
                }
                else if (followType.Equals("带看租房"))
                {
                    fieldnames.Add("[mark3]");
                }
               
                fieldnames.Add("cfollow");
                
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.UpdateQuantification(qId, fieldnames.ToArray(), db);
                }
            }
        }
        public static void UpdateCustomerFollowQuantification(string followType, float totalSum,int cfollow,int userId, string userName,int deptId,string deptName)
        {
            
            int qId = GetTodayQuantification(userId);

            if (qId == 0)
            {
               
                string outmsg = string.Empty;
                int newid = 0;
                using (_Database db = new _Database("estate"))
                {
                    float  looked = 0;                         float mark3 = 0;                          if (followType.Equals("带看售房"))
                    {
                        looked = totalSum;
                    }
                    else if (followType.Equals("带看租房"))
                    {
                        mark3 =  totalSum;
                    }

                    QuantificationDAL.p_quantification_insert2(deptId, deptName, userId, userName, 0, cfollow, 0, looked, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, mark3, 0, 0, out outmsg, out newid, db);
                }
            }
            else
            {
                List<string> fieldnames = new List<string>();
                if (followType.Equals("带看售房"))
                {
                    fieldnames.Add("[looked]");
                }
                else if (followType.Equals("带看租房"))
                {
                    fieldnames.Add("[mark3]");
                }

                fieldnames.Add("cfollow");

                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.UpdateQuantification(qId, totalSum, cfollow, fieldnames.ToArray(), db);
                }
            }
        }

                                public static void UpdateCustomerAlertQuantification(string AlertType)
        {
            int userId = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            int qId = GetTodayQuantification(userId);

            if (qId == 0)
            {
                int deptId = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);
                string deptName = _Utility.GetInfoBySession().m_sDeptName;
                string userName = _Utility.GetInfoBySession().m_sUserName;
                string outmsg = string.Empty;
                int newid = 0;
                using (_Database db = new _Database("estate"))
                {
                    if (AlertType.Equals("约看"))
                    {
                        QuantificationDAL.p_quantification_insert(deptId, deptName, userId, userName, 0, 0, 1, 0, 0, 0, 0, 0,
                            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, out outmsg, out newid, db);
                    }
                }
            }
            else
            {
                List<string> fieldnames = new List<string>();
                if (AlertType.Equals("约看"))
                {
                    fieldnames.Add("[look]");
                }
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.UpdateQuantification(qId, fieldnames.ToArray(), db);
                }
            }
        }

                                                                                        public static void UpdateCustomerOrHousingQuantification(int typeid,int tableId,int visit,int handbill,int EarnestMoney,int talks,int afterservice ) 
        {
            int userId = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            int deptId = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);
            string deptName = _Utility.GetInfoBySession().m_sDeptName;
            string userName = _Utility.GetInfoBySession().m_sUserName;

            int qId = GetTodayQuantification(userId);

            if (qId == 0)
            {
                string outmsg = string.Empty;
                int newid = 0;
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.p_quantification_insert1(typeid, tableId, deptId, deptName, userId, userName, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, visit, handbill, EarnestMoney, talks, afterservice,out outmsg,out newid,db);
                }
            }
            else
            {
                                List<string> fieldnames = new List<string>();
                if (typeid == 19) 
                {
                    fieldnames.Add("visit");
                }
                else if (typeid == 20)
                {
                    fieldnames.Add("handbill");
                }
                else if (typeid == 21) 
                {
                    fieldnames.Add("EarnestMoney");
                }
                else if (typeid == 22) 
                {
                    fieldnames.Add("talks");
                }
                else if (typeid == 23) 
                {
                    fieldnames.Add("afterservice");
                }
                using (_Database db = new _Database("estate"))
                {
                    QuantificationDAL.UpdateQuantification(typeid, tableId, userId, deptId, deptName, userName, qId, fieldnames.ToArray(), db);

                }
            }


        }

                                                                public static int isExitByTypeId(int typeid,int Id) 
        {
            using (_Database db = new _Database("estate")) 
            {
                return ZLDC.DAL.Estate.QuantificationDAL.isExitByTypeId(typeid,Id, db);
            }
        }

    }
}
